package com.jzo2o.customer.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.BadRequestException;
import com.jzo2o.common.model.CurrentUser;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.common.utils.UserContext;
import com.jzo2o.customer.enums.CertificationStatusEnum;
import com.jzo2o.customer.mapper.WorkerCertificationAuditMapper;
import com.jzo2o.customer.mapper.WorkerCertificationMapper;
import com.jzo2o.customer.model.domain.WorkerCertification;
import com.jzo2o.customer.model.domain.WorkerCertificationAudit;
import com.jzo2o.customer.model.dto.WorkerCertificationUpdateDTO;
import com.jzo2o.customer.model.dto.request.CertificationAuditReqDTO;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditAddReqDTO;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.model.dto.response.WorkerCertificationAuditResDTO;
import com.jzo2o.customer.service.IWorkerCertificationService;
import com.jzo2o.mysql.utils.PageUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 服务人员认证信息表 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-09-06
 */
@Service
public class WorkerCertificationServiceImpl extends ServiceImpl<WorkerCertificationMapper, WorkerCertification> implements IWorkerCertificationService {

    @Resource
    private WorkerCertificationMapper workerCertificationMapper;
    @Resource
    private WorkerCertificationAuditMapper workerCertificationAuditMapper;


    /**
     * 根据服务人员id更新
     *
     * @param workerCertificationUpdateDTO 服务人员认证更新模型
     */
    @Override
    public void updateById(WorkerCertificationUpdateDTO workerCertificationUpdateDTO) {
        LambdaUpdateWrapper<WorkerCertification> updateWrapper = Wrappers.<WorkerCertification>lambdaUpdate()
                .eq(WorkerCertification::getId, workerCertificationUpdateDTO.getId())
                .set(WorkerCertification::getCertificationStatus, workerCertificationUpdateDTO.getCertificationStatus())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getName()), WorkerCertification::getName, workerCertificationUpdateDTO.getName())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getIdCardNo()), WorkerCertification::getIdCardNo, workerCertificationUpdateDTO.getIdCardNo())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getFrontImg()), WorkerCertification::getFrontImg, workerCertificationUpdateDTO.getFrontImg())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getBackImg()), WorkerCertification::getBackImg, workerCertificationUpdateDTO.getBackImg())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getCertificationMaterial()), WorkerCertification::getCertificationMaterial, workerCertificationUpdateDTO.getCertificationMaterial())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getCertificationTime()), WorkerCertification::getCertificationTime, workerCertificationUpdateDTO.getCertificationTime());
        super.update(updateWrapper);
    }

    /**
     * 服务人员认证审核添加
     *
     * @param workerCertificationAuditAddReqDTO 服务人员认证审核添加模型
     */
    @Override
    public void workerCertificationAuditAdd(WorkerCertificationAuditAddReqDTO workerCertificationAuditAddReqDTO) {
        //新增服务人员认证信息\
        Long id = UserContext.getCurrentUser().getId();
        WorkerCertification workerCertificationBuild = WorkerCertification.builder()
                .id(id)
                .name(workerCertificationAuditAddReqDTO.getName())
                .idCardNo(workerCertificationAuditAddReqDTO.getIdCardNo())
                .frontImg(workerCertificationAuditAddReqDTO.getFrontImg())
                .backImg(workerCertificationAuditAddReqDTO.getBackImg())
                .certificationMaterial(workerCertificationAuditAddReqDTO.getCertificationMaterial())
                .certificationStatus(CertificationStatusEnum.PROGRESSING.getStatus())
                .build();
        baseMapper.insert(workerCertificationBuild);
        //新增服务人员认证审核信息
        WorkerCertificationAudit bean = BeanUtils.copyBean(workerCertificationAuditAddReqDTO, WorkerCertificationAudit.class);
        bean.setServeProviderId(id);
        bean.setAuditStatus(0);

        //存在就更新不存在就插入
        if (ObjectUtil.isNull(workerCertificationAuditMapper.selectOne(new QueryWrapper<WorkerCertificationAudit>().eq("id", id)))) {
            workerCertificationAuditMapper.insert(bean);
            baseMapper.insert(workerCertificationBuild);
        } else {
            workerCertificationAuditMapper.updateById(bean);
            baseMapper.updateById(workerCertificationBuild);
        }
    }

    /**
     * 获取当前服务人员认证审核拒绝原因
     *
     * @return 拒绝原因
     */
    @Override
    public String checkRejectReason() {
        //获取当前服务人员id
        Long serveProviderId = UserContext.currentUserId();
        //查询当前服务人员认证审核信息
        LambdaQueryWrapper<WorkerCertificationAudit> wcaQueryWrapper = Wrappers.<WorkerCertificationAudit>lambdaQuery()
                .eq(WorkerCertificationAudit::getAuditStatus, 1)
                .eq(WorkerCertificationAudit::getServeProviderId, serveProviderId);
        WorkerCertificationAudit workerCertificationAudit = workerCertificationAuditMapper.selectOne(wcaQueryWrapper);
        return workerCertificationAudit.getRejectReason();
    }

    /**
     * 服务人员分页查询
     *
     * @param workerCertificationAuditPageQueryReqDTO 分页查询模型
     * @return 分页结果
     */
    @Override
    public PageResult<WorkerCertificationAuditResDTO> pageQuery(WorkerCertificationAuditPageQueryReqDTO workerCertificationAuditPageQueryReqDTO) {
        //使用mybatis plus分页查询
        Page<WorkerCertificationAudit> page = PageUtils.parsePageQuery(workerCertificationAuditPageQueryReqDTO, WorkerCertificationAudit.class);
        Page<WorkerCertificationAudit> selectPage = workerCertificationAuditMapper.selectPage(page, new QueryWrapper<>());

        PageResult<WorkerCertificationAuditResDTO> workerCertificationAuditPage = PageUtils.toPage(selectPage, WorkerCertificationAuditResDTO.class);
        return workerCertificationAuditPage;
    }

    @Override
    public void workerCertificationAudit(Long id, CertificationAuditReqDTO certificationAuditReqDTO) {
        //获取当前审核人员id
        Long auditorId = UserContext.currentUserId();
        CurrentUser currentUser = UserContext.getCurrentUser();
        //查询当前服务人员认证信息
        LambdaQueryWrapper<WorkerCertificationAudit> wcaQueryWrapper = Wrappers.<WorkerCertificationAudit>lambdaQuery()
                .eq(WorkerCertificationAudit::getAuditStatus, 0)
                .eq(WorkerCertificationAudit::getServeProviderId, id);
        WorkerCertificationAudit workerCertificationAudit = workerCertificationAuditMapper.selectOne(wcaQueryWrapper);
        workerCertificationAudit.setAuditorId(auditorId);
        workerCertificationAudit.setAuditorName(currentUser.getName());
        workerCertificationAudit.setAuditTime(LocalDateTime.now());
        workerCertificationAudit.setAuditStatus(1);
        if (ObjectUtil.isNotEmpty(certificationAuditReqDTO.getCertificationStatus())) {
            workerCertificationAudit.setCertificationStatus(certificationAuditReqDTO.getCertificationStatus());
        } else {
            throw new BadRequestException("无认证状态");
        }
        if (StrUtil.isNotBlank(certificationAuditReqDTO.getRejectReason())) {
            workerCertificationAudit.setRejectReason(certificationAuditReqDTO.getRejectReason());
        }
        WorkerCertification workerCertification = workerCertificationMapper.selectById(workerCertificationAudit.getServeProviderId());
        workerCertification.setCertificationStatus(certificationAuditReqDTO.getCertificationStatus());
        workerCertificationMapper.updateById(workerCertification);

        workerCertificationAuditMapper.updateById(workerCertificationAudit);

    }
}
